package com.ruoyi.web.controller.module;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.SysUserWxAdmin;
import com.ruoyi.system.service.system.ISysDeptService;
import com.ruoyi.system.service.system.ISysUserService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.module.ISysUserWxAdminService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 系统管理员微信绑定Controller
 *
 * @author ruoyi
 * @date 2022-05-15
 */
@Validated
@Api(value = "系统管理员微信绑定控制器", tags = {"系统管理员微信绑定管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/system/wx/admin")
public class SysUserWxAdminController extends BaseController {

    private final ISysUserWxAdminService sysUserWxAdminService;
    private final ISysUserService sysUserService;
    private final ISysDeptService sysDeptService;

    /**
     * 查询系统管理员微信绑定列表
     */
    @ApiOperation("查询系统管理员微信绑定列表")
    @PreAuthorize("@ss.hasPermi('system:admin:list')")
    @GetMapping("/list")
    public TableDataInfo<SysUserWxAdmin> list(@Validated SysUserWxAdmin sysUserWxAdmin) {
        QueryWrapper<SysUserWxAdmin> qw = new QueryWrapper<>();
        if(sysUserWxAdmin.getSysUserId()!=null) {
            qw.eq("sys_user_id", sysUserWxAdmin.getSysUserId());
        }
        TableDataInfo<SysUserWxAdmin> td = PageUtils.buildDataInfo(sysUserWxAdminService.page(PageUtils.buildPage(),qw));
        List<SysUserWxAdmin> list = td.getRows();
        return td;
    }

    /**
     * 导出系统管理员微信绑定列表
     */
    @ApiOperation("导出系统管理员微信绑定列表")
    @PreAuthorize("@ss.hasPermi('system:admin:export')")
    @Log(title = "系统管理员微信绑定", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<SysUserWxAdmin> export(@Validated SysUserWxAdmin sysUserWxAdmin) {
        List<SysUserWxAdmin> list = sysUserWxAdminService.list();
        ExcelUtil<SysUserWxAdmin> util = new ExcelUtil<SysUserWxAdmin>(SysUserWxAdmin.class);
        return util.exportExcel(list, "系统管理员微信绑定");
    }

    /**
     * 获取系统管理员微信绑定详细信息
     */
    @ApiOperation("获取系统管理员微信绑定详细信息")
    @PreAuthorize("@ss.hasPermi('system:admin:query')")
    @GetMapping("/{id}")
    public AjaxResult<SysUserWxAdmin> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        SysUserWxAdmin sysUserWxAdmin = sysUserWxAdminService.getById(id);
        return AjaxResult.success(sysUserWxAdmin);
    }

    /**
     * 新增系统管理员微信绑定
     */
    @ApiOperation("新增系统管理员微信绑定")
    @PreAuthorize("@ss.hasPermi('system:admin:add')")
    @Log(title = "系统管理员微信绑定", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody SysUserWxAdmin sysUserWxAdmin) {
        SysUser sysUser = sysUserService.getById(sysUserWxAdmin.getSysUserId());
        if(sysUser!=null){
            sysUserWxAdmin.setName(sysUser.getNickName());
        }
        SysDept sysDept = sysDeptService.getById(sysUserWxAdmin.getDeptId());
        if(sysDept!=null){
            sysUserWxAdmin.setDeptName(sysDept.getDeptName());
        }
        return toAjax(sysUserWxAdminService.save(sysUserWxAdmin) ? 1 : 0);
    }

    /**
     * 修改系统管理员微信绑定
     */
    @ApiOperation("修改系统管理员微信绑定")
    @PreAuthorize("@ss.hasPermi('system:admin:edit')")
    @Log(title = "系统管理员微信绑定", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody SysUserWxAdmin sysUserWxAdmin) {
        SysUser sysUser = sysUserService.getById(sysUserWxAdmin.getSysUserId());
        if(sysUser!=null){
            sysUserWxAdmin.setName(sysUser.getNickName());
        }
        SysDept sysDept = sysDeptService.getById(sysUserWxAdmin.getDeptId());
        if(sysDept!=null){
            sysUserWxAdmin.setDeptName(sysDept.getDeptName());
        }
        return toAjax(sysUserWxAdminService.updateById(sysUserWxAdmin) ? 1 : 0);
    }

    /**
     * 删除系统管理员微信绑定
     */
    @ApiOperation("删除系统管理员微信绑定")
    @PreAuthorize("@ss.hasPermi('system:admin:remove')")
    @Log(title = "系统管理员微信绑定" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(sysUserWxAdminService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
